local desc_huanhua_2v2 = [[
  ## 幻化之战 2v2版本简介

  ---

  ## 身份说明

  游戏由两名忠臣和两名反贼进行，胜利目标为击杀所有敌人。

  座位排列可能是忠-反-反-忠或者忠-反-忠-反，以及对应的反贼在一号位的情况。

  ---

  ## 遗产

  当一名角色的队友死亡后，其执行〖幻化〗。

  ---

  ## 新生

  所有角色以自己的头像为武将，体力上限、体力值会被调整到4点、护甲会归零。

  ---

  ## 幻化

  〖幻化〗：从3个技能选择一个获得，或者摸两张牌。

  每轮开始时，存活角色各自执行〖幻化〗。

  ---

]]

local H = require "packages/activities/HuanhuaFunc"

---队友的遗产
---@param room Room  @ 所在房间
---@param player ServerPlayer @ 执行者
local function Reward(room, player)
  local skill_names = H.getSkills(room, 3)
  if #skill_names == 0 then room:gameOver("") end
  local choices = {}
  table.insertTableIfNeed(choices, skill_names)
  table.insert(choices, "DrawTwo")

  local selected_skills = {}
  local choice = room:askForChoice(player, choices, "huanhua_choice", "#huanhua-choose", true)
  if not choice or choice == "DrawTwo" then
    player:drawCards(2)
  else
    room:handleAddLoseSkills(player, choice)
    table.insert(selected_skills, choice)
  end
  H.returnToSkillPool(room, table.filter(skill_names, function(s) return not table.contains(selected_skills, s) end))
end

  --模式逻辑
local huanhua_2v2_getLogic = function()
  ---@class GameLogic
  local huanhua_2v2_logic = GameLogic:subclass("huanhua_2v2_logic")
  -- 身份详情
  function huanhua_2v2_logic:assignRoles()
    local room = self.room
    local roles = table.random {
      { "loyalist", "rebel", "rebel", "loyalist" },
      { "rebel", "loyalist", "loyalist", "rebel"},
    }

    for i = 1, 4 do
      local p = room.players[i]
      p.role = roles[i]
      p.role_shown = true
      room:broadcastProperty(p, "role")
    end

    room.players[1]:addBuddy(room.players[4])
    room.players[4]:addBuddy(room.players[1])
    room.players[2]:addBuddy(room.players[3])
    room.players[3]:addBuddy(room.players[2])

    self.start_role = roles[1]
    -- for adjustSeats
    room.players[1].role = "lord"
  end
  -- 皮套自备
  function huanhua_2v2_logic:chooseGenerals()
    local room = self.room
    local is_deputy_mode = (room.settings.enableDeputy == 2)
    local lord = room:getLord()
    room.current = lord
    lord.role = self.start_role
    local players = room.players
    for _, p in ipairs(players) do
      local avatar = p._splayer:getAvatar()
      if avatar == "anjiang" then avatar = "guanyu" end
      local avatar_general = Fk.generals[avatar] or Fk.generals["guanyu"]
      room:setPlayerGeneral(p, avatar_general.name, true)
      room:broadcastProperty(p, "general")
      room:setPlayerProperty(p, "shield", 0)
      room:setPlayerProperty(p, "maxHp", 4)
      room:setPlayerProperty(p, "hp", 4)
    end
    room:doBroadcastNotify("ShowToast", Fk:translate("huanhua_intro"))
    room:setTag("SkipNormalDeathProcess", true)
  end
  -- 体力初始化（上面已经做了）
  function huanhua_2v2_logic:broadcastGeneral()

  end
  -- 技能初始化
  function huanhua_2v2_logic:attachSkillToPlayers()

  end

  return huanhua_2v2_logic
end

  --幻化2v2
local huanhua_2v2_rule = fk.CreateTriggerSkill{
  name = "#huanhua_2v2_rule",
  priority = 0.001,
  refresh_events = {fk.GameStart, fk.DrawInitialCards, fk.DrawNCards, fk.GameOverJudge, fk.Deathed, fk.RoundStart},
  can_refresh = function(self, event, target, player, data)
    return target == player
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    -- 一号位 首回合额定摸牌数-1
    if event == fk.DrawNCards then
      if player.seat == 1 and player:getMark(self.name) == 0 then
        room:addPlayerMark(player, self.name, 1)
        data.n = data.n - 1
      end
    -- 四号位 初始手牌+1
    elseif event == fk.DrawInitialCards then
      if player.seat == 4 then
        data.num = data.num + 1
      end
    -- 构建技能池
    elseif event == fk.GameStart then
      H.createSkillPool(room)
    -- 结束判定
    elseif event == fk.GameOverJudge then
      local winner = Fk.game_modes[room.settings.gameMode]:getWinner(player)
      if winner ~= "" then
        room:gameOver(winner)
        return true
      end
    -- 幻化！
    elseif event == fk.RoundStart then
      H.doHuanhua(room)
    -- 遗产
    else
      for _, p in ipairs(room.alive_players) do
        if p.role == player.role then
          Reward(room, p)
        end
      end
    end
  end,
}
Fk:addSkill(huanhua_2v2_rule)

--游戏模式
local huanhua_2v2_mode = fk.CreateGameMode{
  name = "huanhua_2v2_mode",
  minPlayer = 4,
  maxPlayer = 4,
  rule = huanhua_2v2_rule,
  logic = huanhua_2v2_getLogic,
  surrender_func = function(self, playedTime)
    local surrenderJudge = { { text = "time limitation: 2 min", passed = playedTime >= 120 },
    { text = "2v2: left you alive", passed = table.find(Fk:currentRoom().players, function(p)
      return p.role == Self.role and p.dead and p.rest == 0
    end) and true } }
    return surrenderJudge
  end,
  winner_getter = function(self, victim)
    if not victim.surrendered and victim.rest > 0 then
      return ""
    end
    local room = victim.room
    local alive = table.filter(room.players, function(p) ---@type Player[]
      return not p.surrendered and not (p.dead and p.rest == 0)
    end)
    local winner = alive[1].role
    for _, p in ipairs(alive) do
      if p.role ~= winner then
        return ""
      end
    end
    return winner
  end,
}
Fk:loadTranslationTable{
  ["huanhua_2v2_mode"] = "幻化2v2",
  [":huanhua_2v2_mode"] = desc_huanhua_2v2,
  ["#huanhua_2v2_rule"] = "幻化2v2",
  
  ["huanhua_2v2_intro"] = "选用巧妙的技能组合，合力击败对手吧！",
  ["time limitation: 2 min"] = "游戏时长达到2分钟",
  ["2v2: left you alive"] = "你所处队伍仅剩你存活",

  ["H_reward"] = "队友的遗产",
  ["H_reward-choosing"] = "遗产：选择一项技能获得"
}

return huanhua_2v2_mode
